package com.ecoolex.ying.space.core.manager;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.ying.space.core.dao.UserWatchDAO;
import com.ecoolex.ying.space.entity.UserIdentityAuthentication;
import com.ecoolex.ying.space.entity.UserWatch;
import com.ecoolex.ying.space.entity.UserWatch.Column;
import com.ecoolex.ying.space.entity.UserWatchExample;
import com.ecoolex.ying.space.enums.UserIdentityAuthenticationDomainType;
import com.ecoolex.ying.space.pojo.QueryUserWatchDTO;
import com.ecoolex.ying.space.pojo.QueryUserWatchFansDTO;


@Service
public class UserWatchManager {

    @Autowired
    private UserWatchDAO dao;

    @Autowired
    private UserIdentityAuthenticationManager userIdentityAuthenticationManager;

    public UserWatch getById(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    public UserWatch getUserWatchByTwoUserId(Integer userId, Integer watchUserId) {
        UserWatchExample example = new UserWatchExample();
        UserWatchExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(UserWatch.NOT_DELETED);
        criteria.andUserIdEqualTo(userId);
        criteria.andWatchUserIdEqualTo(watchUserId);
        return dao.selectOneByExample(example);
    }

    public Long watchCount(Integer userId) {
        return dao.countWithUserWatch(userId, new QueryUserWatchDTO());
    }

    public Long FansCount(Integer userId) {
        return dao.countWithUserWatchFans(userId, new QueryUserWatchFansDTO());
    }

    public Pagination<ConsumerMember> queryWatchList(Integer userId, QueryUserWatchDTO query, Sorting[] sorts, Page page) {
        long count = dao.countWithUserWatch(userId, query);
        return Pagination.create(page,
            count,
            () -> dao.queryWithUserWatch(userId, query, Sortings.builder(sorts).toSql(), page));
    }

    public Pagination<ConsumerMember> queryFansList(Integer userId, QueryUserWatchFansDTO query, Sorting[] sorts, Page page) {
        long count = dao.countWithUserWatchFans(userId, query);
        return Pagination.create(page,
            count,
            () -> dao.queryWithUserWatchFans(userId, query, Sortings.builder(sorts).toSql(), page));
    }

    @Transactional
    public UserWatch addUserWatch(UserWatch record, String createUser) {
        record.setMutualWatch(false);
        record.setDeleted(UserWatch.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        // 设置相互关注
        dao.settingMutualWatch(record.getUserId(), record.getWatchUserId(), createUser);
        return record;
    }

    @Transactional
    public void deleteUserWatch(UserWatch record, String updateUser) {
        // 删除关注信息
        record.setDeleted(UserWatch.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record,
            UserWatch.Column.deleted,
            UserWatch.Column.updateUser,
            UserWatch.Column.updateTime);

        // 修改相互关注参数
        UserWatch update = new UserWatch();
        update.setMutualWatch(false);
        update.setUpdateUser(updateUser);
        update.setUpdateTime(Date.from(Instant.now()));

        UserWatchExample example = new UserWatchExample();
        example.createCriteria().andUserIdEqualTo(record.getWatchUserId()).andWatchUserIdEqualTo(
            record.getUserId()).andDeletedEqualTo(UserWatch.NOT_DELETED);
        dao.updateByExampleSelective(update, example, Column.mutualWatch, Column.updateUser, Column.updateTime);
    }

    public void addDefaultWatch(Integer userId, String createUser) {
        List<UserIdentityAuthentication> list = userIdentityAuthenticationManager.queryByDomainType(
            UserIdentityAuthenticationDomainType.OFFICIALPLATFORM);
        for (UserIdentityAuthentication userIdentityAuthentication : list) {
            UserWatch userWatch = new UserWatch();
            userWatch.setUserId(userId);
            userWatch.setWatchUserId(userIdentityAuthentication.getUserId());
            userWatch.setMutualWatch(false);
            userWatch.setDeleted(UserWatch.NOT_DELETED);
            userWatch.setCreateUser(createUser);
            userWatch.setCreateTime(Date.from(Instant.now()));
            userWatch.setUpdateUser(createUser);
            userWatch.setUpdateTime(Date.from(Instant.now()));
            dao.insert(userWatch);
        }
    }
}